#include "ProgressBar.h"

void ProgressBar::draw(HDC hdc)
{
	
	if(updated == false)
	{
		
		int lineH = 2;

		int center = (int)((width-(PB_SPACE*2)) * ((float)progress/100));

		Bitmap bmp(width,height);
		Graphics *fromImg = Graphics::FromImage(&bmp);


		SolidBrush  brush(Color(255, 0, 0, 255));
		
		

		SolidBrush  progr(Color(255, 0, 162, 232));
		SolidBrush  left(Color(255, 195, 195, 195));

	
		SolidBrush  brushSlider(Color(255, 85, 85, 85));
		SolidBrush  brushBackground(Color(255, 34, 34, 34));
	
		Pen      penBorder(Color(255, 0, 0, 0));


		fromImg->DrawRectangle(&penBorder, 0, 0, width-1, height-1);
		fromImg->FillRectangle(&brushBackground, 1, 1, width-2, height-2);
	
		//fromImg->FillRectangle(&progr, 1, height/2-(lineH/2) , center, lineH);
		//fromImg->FillRectangle(&left, center, height/2-(lineH/2) , width-2, lineH);

		/*fromImg->FillRectangle(&progr, 1 + PB_SPACE, height/2-(lineH/2) , center, lineH);
		fromImg->FillRectangle(&left, center+1+ PB_SPACE, height/2-(lineH/2) , (width-center)-2-2*PB_SPACE, lineH);*/

		//int c_cube = (int)((width) * ((float)progress/100)) - PB_CUBE/2;
		int c_cube = center - (int)((float)PB_CUBE/2+0.5) + PB_SPACE;


		fromImg->FillRectangle(&brushSlider, c_cube, 1, PB_CUBE, height-2);
		fromImg->DrawRectangle(&penBorder, c_cube, 0, PB_CUBE-1, height-1);

	
		Graphics    graphics(hdc);
		graphics.DrawImage(&bmp, x, y, width, height);

		delete fromImg;
		updated = true;
	}
	if(is_mouse_down)
		{
			calculate();
		}
}

float ProgressBar::setProgress(float p)
{
	float r = -1;
	if(!is_mouse_down || lastMousePoint1 == lastMousePoint2)
	{
		if(p < 0) p = 0.0;
		if(p > 100) p = 100.0;
		progress = p;
	}
	else
	{
		r = progress;
	}


	return r;
}

float ProgressBar::getProgress()
{
	return progress;
}

void ProgressBar::update()
{
	updated = false;

	RECT* lpRect = new RECT;

	lpRect->top = y;
	lpRect->left = x; 

	lpRect-> bottom = y+height;
	lpRect->right = x+width;


	InvalidateRect(hWnd, lpRect, false);

	delete[] lpRect;
}


void ProgressBar::mouseDown()
{

	LPPOINT p = new POINT;
	GetCursorPos(p);
	ScreenToClient(hWnd, p);

	//drawMode++;
		//if(drawMode >= drawModeCount) drawMode = 0;
	if(p->x >= x+1 && p->x <= x + width-1 &&
		p->y >= y && p->y <= y + height)
	{
		is_mouse_down = true;
		

		progress = calculate();
		
		lastMousePoint1 = 0;
		lastMousePoint2 = 1;
	}

	delete[] p;


}



void ProgressBar::mouseUp()
{
	is_mouse_down = false;
}

int ProgressBar::calculate()
{
	LPPOINT p = new POINT;
	GetCursorPos(p);
	ScreenToClient(hWnd, p);
	
	int tmp = p->x - x - PB_SPACE;
	lastMousePoint1 = lastMousePoint2;
	lastMousePoint2 = tmp;


	if(p->x >= x+1+PB_SPACE && p->x <= x + width-1-PB_SPACE)
	{
		
		if(lastMousePoint1 != tmp) progress = (float)tmp/(width-2*PB_SPACE)*100;
	}	
	else
	{
		
		if(p->x < x+1 + PB_SPACE) progress = 0.01;
		if(p->x > x + width-1 - PB_SPACE) progress = 100;
		
	}

	

	delete[] p;

	if(lastMousePoint1 != lastMousePoint2) 
	{ 
		SendMessage(hWnd, WM_COMMAND, control_id, 0);
		update();
	}
	return 0;
}